﻿using System;   
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleWebsite.Models;

namespace SaleWebsite.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View(Product.ConvertAll(Product.GetAll()));
        }
        [HttpGet]
        public ActionResult insert()
        {

            return View();


        }
        [HttpPost]
        public ActionResult insert(Product product)
        {
           try
           {
               if (ModelState.IsValid)
               {
                   if (product.Insert()>0)
                   {
                       return RedirectToAction("Index");
                   }
               }else
               {
                   return View("insert", product);
               }
               
               
               
           }
           catch (System.Exception ex)
           {
               ModelState.AddModelError("error", ex.Message);
           }
           return View("insert");

        }
        public ActionResult Save_provider(string name, string address, string phone, string id_cart, string email, ulong id)
        {
            try
            {
                Provider item = new Provider() { Id = id, Name = name, Address = address, Phone = phone, Id_cart = id_cart, Email = email };
                if (item.Save() > 0)
                {
                    //return Json(new { error = "update Faile" });
                    return PartialView("provider_row", item);
                }
                else
                {
                    return Json(new { error = "update Faile" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }
        public JsonResult delete(ulong id)
        {
            Product item = new Product() { Id = id };
            if (item.Delete() > 0)
            {
                return Json(new { status = "success" });
            }
            else
            {
                return Json(new { status = "not" });
            }

        }

    }
}
